﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OracleCompare
{
    public partial class Form2 : Form
    {
        public string tableName = "";
        private List<TableCol> col;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            if (!Configs.SameTable.Contains(tableName)) { this.Close(); return; }
            label2.Text = tableName;
            col = Configs.SourceTable.Where(m => m.TableName.Equals(tableName)).OrderBy(m => m.Index).ToList();
            Configs.DifferentList = new List<Dictionary<string, string>>();
            Configs.SourceList = new List<Dictionary<string, string>>();
            Configs.TargetList = new List<Dictionary<string, string>>();
            Configs.SameList = new List<Dictionary<string, string>>();
            ReadTable();
            label4.Text = Configs.DifferentList.Count().ToString();
            label6.Text = Configs.SourceList.Count().ToString();
            label8.Text = Configs.TargetList.Count().ToString();
            label10.Text = Configs.SameList.Count().ToString();
            if (!Configs.DifferentList.Any()) { button1.Visible = false; }
            if (!Configs.SourceList.Any()) { button2.Visible = false; }
            if (!Configs.TargetList.Any()) { button3.Visible = false; }
            if (!Configs.SameList.Any()) { button4.Visible = false; }
        }

        private void ReadTable()
        {
            Configs.SourceAll = GetAll(Configs.SourceConnection);
            Configs.TargetAll = GetAll(Configs.TargetConnection);
            var keys = col.Where(m => m.IsKey).Select(m => m.ColumnName).Distinct();
            foreach (var s in Configs.SourceAll)
            {
                var list = Configs.TargetAll;
                if (keys.Any())
                {
                    //Dictionary<string, string> key = new Dictionary<string, string>();
                    foreach (var k in keys)
                    {
                        //key.Add(k, s[k]);
                        list = list.Where(m => m[k].Equals(s[k])).ToList();
                    }
                    var t = list.FirstOrDefault();
                    if (null == t) { Configs.SourceList.Add(s); }
                    else
                    {
                        bool isSame = true;
                        foreach (var k in col.Select(m => m.ColumnName))
                        {
                            if (keys.Contains(k)) continue;
                            s.Add("t" + k, t[k]);
                            if (s[k] != t[k]) isSame = false;
                        }
                        if (isSame) { Configs.SameList.Add(s); }
                        else
                        {
                            //Configs.DifferentList.Add(key);
                            Configs.DifferentList.Add(s);
                        }
                    }
                }
                else
                {
                    foreach (var k in col.Select(m => m.ColumnName))
                    {
                        list = list.Where(m => m[k] == s[k]).ToList();
                    }
                    var t = list.FirstOrDefault();
                    if (null == t) { Configs.SourceList.Add(s); }
                    else { Configs.SameList.Add(s); }
                }
            }
            foreach (var t in Configs.TargetAll)
            {
                var list = Configs.SourceAll;
                if (keys.Any())
                {
                    foreach (var k in keys)
                    {
                        list = list.Where(m => m[k].Equals(t[k])).ToList();
                    }
                    var s = list.FirstOrDefault();
                    if (null == s) { Configs.TargetList.Add(t); }
                }
                else
                {
                    foreach (var k in col.Select(m => m.ColumnName))
                    {
                        list = list.Where(m => m[k] == t[k]).ToList();
                    }
                    var s = list.FirstOrDefault();
                    if (null == s) { Configs.TargetList.Add(t); }
                }
            }
        }

        private List<Dictionary<string, string>> GetAll(string connectionString)
        {
            List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
            using (OracleConnection conn = new OracleConnection(connectionString))
            {
                try
                {
                    conn.Open();
                    OracleCommand cmd = conn.CreateCommand();
                    cmd.CommandText = string.Format("SELECT * FROM \"{0}\"", tableName);
                    OracleDataReader odr = cmd.ExecuteReader();
                    while (odr.Read())
                    {
                        int i = 0;
                        Dictionary<string, string> entity = new Dictionary<string, string>();
                        foreach (var t in col)
                        {
                            entity.Add(t.ColumnName, null == odr[i] ? null : odr[i].ToString());
                            i++;
                        }
                        list.Add(entity);
                    }
                    odr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("表读取错误：" + ex.Message, "错误");
                }
                finally
                {
                    conn.Clone();
                }
            }
            return list;
        }

        private void Read_Click(object sender, EventArgs e)
        {
            Form3 form = new Form3();
            form.type = byte.Parse(((Button)sender).Tag.ToString());
            form.col = col;
            form.ShowDialog();
        }
    }
}
